package com.noble.meether.mail.ui.friend.request;

import android.content.Context;

import com.noble.glamour.bean.center.user.light.UserLight;
import com.noble.glamour.bean.center.user.light.UserLightList;
import com.noble.glamour.module.logic.application.ModuleMgr;
import com.noble.glamour.module.logic.request.HttpResponse;
import com.noble.glamour.module.logic.request.RequestComplete;
import com.noble.library.view.base.BasePanel;
import com.noble.library.view.custom.state.CustomStatusListView;
import com.noble.library.widget.ListView.xlistview.ExListView;
import com.noble.meether.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 我发起的好友
 */
public class FriendMyRequestPanel extends BasePanel implements RequestComplete, ExListView.IXListViewListener {
    private CustomStatusListView customStatusListView;
    private ExListView exListView;

    private FriendMyPanelAdapter adapter;
    private List<UserLight> data = new ArrayList<>();

    private int page = 1;

    public FriendMyRequestPanel(Context context) {
        super(context);
        setContentView(R.layout.panel_request_friend);
        initView();
        onRefresh();
    }


    private void initView() {
        customStatusListView = (CustomStatusListView) findViewById(R.id.myfriend_list);
        exListView = customStatusListView.getExListView();
        exListView.setXListViewListener(this);
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        adapter = new FriendMyPanelAdapter(getContext(), data);
        exListView.setAdapter(adapter);
        customStatusListView.showLoading();
    }

    @Override
    public void onRefresh() {
        exListView.setPullRefreshEnable(true);
        exListView.setPullLoadEnable(true);
        page = 1;
        ModuleMgr.getCommonMgr().reqAddFriend(page, this);
    }

    @Override
    public void onLoadMore() {
        page++;
        ModuleMgr.getCommonMgr().reqAddFriend(page, this);
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.isOk()) {
            UserLightList infoList = (UserLightList) response.getBaseData();

            if (infoList.getLightList().size() != 0) {
                if (page == 1) {
                    if (data.size() != 0) {
                        data.clear();
                    }
                }
                data.addAll(infoList.getLightList());
                if (data.size() < 10) {
                    exListView.setPullLoadEnable(false);
                }
                adapter.notifyDataSetChanged();
                customStatusListView.showExListView();
            } else {
                if (page == 1) {
                    customStatusListView.showNoData("暂未添加好友");
                } else {
                    exListView.setPullLoadEnable(false);
                }
            }

            if (page == 1) {
                exListView.stopRefresh();
            } else {
                exListView.stopLoadMore();
            }
        } else {
            customStatusListView.showNoData("请求出错", "重试", view -> {
                customStatusListView.showLoading();
                onRefresh();
            });
        }
    }

    public void release() {
    }
}
